package org.ch03.job.test04;

/**
 * @author XCH
 * @date 2025 - 03 - 26
 */
public class MainTest {
    public static void main(String[] args) {
        // 1. 创建 MyArrayList 实例
        System.out.println("1. 创建 MyArrayList 实例");
        MyArrayList<String> programmingLanguages = new MyArrayList<>();
        MyArrayList<Integer> primeNumbers = new MyArrayList<>(5);

        System.out.println("programmingLanguages 初始大小: " + programmingLanguages.size());
        System.out.println("primeNumbers 初始大小: " + primeNumbers.size());
        System.out.println("programmingLanguages 是否为空: " + programmingLanguages.isEmpty());
        System.out.println("----------------------------------");

        // 2. 添加元素
        System.out.println("2. 添加元素");
        programmingLanguages.add("Java");
        programmingLanguages.add("Python");
        programmingLanguages.add("C++");
        programmingLanguages.add(1, "JavaScript"); // 在索引1处插入

        primeNumbers.add(2);
        primeNumbers.add(3);
        primeNumbers.add(5);
        primeNumbers.add(7);
        primeNumbers.add(11);
        primeNumbers.add(13); // 测试自动扩容

        System.out.println("programmingLanguages 内容: " + programmingLanguages);
        System.out.println("primeNumbers 内容: " + primeNumbers);
        System.out.println("programmingLanguages 当前大小: " + programmingLanguages.size());
        System.out.println("primeNumbers 当前大小: " + primeNumbers.size());
        System.out.println("----------------------------------");

        // 3. 获取元素
        System.out.println("3. 获取元素");
        System.out.println("programmingLanguages 中索引2的元素: " + programmingLanguages.get(2));
        System.out.println("primeNumbers 中索引0的元素: " + primeNumbers.get(0));
        System.out.println("----------------------------------");

        // 4. 修改元素
        System.out.println("4. 修改元素");
        String oldLanguage = programmingLanguages.set(0, "Kotlin");
        System.out.println("被替换的语言: " + oldLanguage);
        System.out.println("修改后的 programmingLanguages: " + programmingLanguages);
        System.out.println("----------------------------------");

        // 5. 删除元素
        System.out.println("5. 删除元素");
        System.out.println("删除前 primeNumbers: " + primeNumbers);
        int removedPrime = primeNumbers.remove(2); // 删除索引2的元素(5)
        System.out.println("被删除的质数: " + removedPrime);
        System.out.println("删除后 primeNumbers: " + primeNumbers);
        System.out.println("当前大小: " + primeNumbers.size());
        System.out.println("----------------------------------");

        // 6. 测试边界条件
        System.out.println("6. 测试边界条件");
        try {
            System.out.println("尝试获取索引10的元素...");
            programmingLanguages.get(10);
        } catch (IndexOutOfBoundsException e) {
            System.out.println("捕获异常: " + e.getMessage());
        }

        try {
            System.out.println("尝试在索引5添加元素...");
            programmingLanguages.add(5, "Ruby");
        } catch (IndexOutOfBoundsException e) {
            System.out.println("捕获异常: " + e.getMessage());
        }
        System.out.println("----------------------------------");

        // 7. 清空列表
        System.out.println("7. 清空列表");
        while (!programmingLanguages.isEmpty()) {
            programmingLanguages.remove(0);
        }
        System.out.println("清空后 programmingLanguages: " + programmingLanguages);
        System.out.println("当前大小: " + programmingLanguages.size());
        System.out.println("是否为空: " + programmingLanguages.isEmpty());
    }
}
